<script setup>
import {onMounted, reactive, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {deleteRank, findRanks} from "@/apis/zyj/ranks.js";
import AddDialog from "@/components/Ranks/AddDialog.vue";
import UpdataDialog from "@/components/Ranks/UpdataDialog.vue";

const formInline = reactive({
  rankNo: '',
  rankArrangement: '',
  rankArrangementName: '',
  salaryCap: '',
  salaryMinimum: ''
})

//初始化数据
function initData() {
  findRanks(formInline, currentPage.value, pageSize.value).then(res => {
    console.log(res)
    tableData.length = 0
    tableData.push(...res.records);
    total.value = res.total;
    currentPage.value = res.current;
  })
}

onMounted(() => {
  initData();
})
//表格数据
const tableData = reactive([])
//分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const addDialog = ref(false)
const updataDialog = ref(false)

const select_order_number = ref('') //表格select选中的条数
const select_orderId = ref([]) //表格select复选框选中的id
const multipleSelection = ref()//选中行的数据
const multipleTable = ref()//用于表格多选框选中的清理
// 搜索按钮
const onSearch = () => {
  initData();
}
//重置按钮
const onReset = () => {
  Object.keys(formInline).forEach(key => formInline[key] = '')
}
//新增按钮
const onAdd = () => {
  addDialog.value = true
}
//修改按钮
const onEdit = (row) => {
  updataDialog.value = true
  if (row.rankNo != null) {
    multipleSelection.value = {...row}
  } else {
    multipleSelection.value = {...multipleSelection.value[0]}
  }
}
//删除按钮
const onDelete = (row) => {
  if (row.rankNo != null) {
    select_order_number.value = "1";
  } else {
    select_order_number.value = multipleSelection.value.length;
  }
  console.log(select_orderId.value)
  ElMessageBox.confirm(
      '是否要删除 ' + select_order_number.value + '条数据',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        if (row.rankNo != null) {
          select_orderId.value = [];
          select_orderId.value.push(row.rankNo)
        }
        deleteRank(select_orderId.value).then((res) => {
          if (res == '成功') {
            initData()
            ElMessage.success('成功删除: ' + select_order_number.value + '条数据')
            select_order_number.value = "0";
            select_orderId.value = [];
            multipleTable.value.clearSelection();
          } else {
            ElMessage.error('系统错误！删除失败')
          }
        })
      })
      .catch(() => {
        ElMessage.info('删除取消')
      })
}
//导出按钮
const onExport = () => {
  // Implement export logic here
  console.log('Export data')
}
//多选
const handleSelectionChange = (val) => {
  // 解决来回切换页面，也无法清除上次选中情况
  multipleSelection.value = val;
  select_order_number.value = multipleSelection.value.length;
  select_orderId.value = [];
  if (val) {
    val.forEach((row) => {
      if (row) {
        select_orderId.value.push(row.rankNo);
      }
    });
  }
}
//关闭弹窗
const closeDialog = () => {
  addDialog.value = false
  updataDialog.value = false
  initData()
}
//分页
const handleSizeChange = (val) => {
  initData()
}
const handleCurrentChange = (val) => {
  initData()
}
</script>

<template>
  <div class="job-rank-management">
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="职级编码">
          <el-input v-model="formInline.rankNo" placeholder="请输入职级编码"/>
        </el-form-item>
        <el-form-item label="职级层次">
          <el-input v-model="formInline.rankArrangement" placeholder="请输入职级层次"/>
        </el-form-item>
        <el-form-item label="职级层次名称">
          <el-input v-model="formInline.rankArrangementName" placeholder="请输入职级层次名称"/>
        </el-form-item>
        <el-form-item label="薪资范围">
          <el-input v-model="formInline.salaryMinimum" placeholder="请输入下限" style="width: 150px;"/>
          <span class="el-input__separator">~</span>
          <el-input v-model="formInline.salaryCap" placeholder="请输入上限" style="width: 150px;"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 20px">
      <div class="table-operations">
        <el-button type="primary" @click="onAdd" plain>新增</el-button>
        <el-button type="success" @click="onEdit" :disabled="select_order_number!=1" plain>修改</el-button>
        <el-button type="danger" @click="onDelete" :disabled="select_order_number==0" plain>删除</el-button>
      </div>

      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
                :row-style="{height: '60px'}" max-height="350px">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="rankNo" label="职级编码"/>
        <el-table-column prop="rankArrangement" label="职级层次"/>
        <el-table-column prop="rankArrangementName" label="职级层次名称"/>
        <el-table-column label="薪资范围（元）">
          <el-table-column prop="salaryMinimum" label="薪资范围下限"/>
          <el-table-column prop="salaryCap" label="薪资范围上限"/>
        </el-table-column>
        <el-table-column prop="remark" label="备注"/>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="onEdit(scope.row)" plain>修改</el-button>
            <el-button size="small" type="danger" @click="onDelete(scope.row)" plain>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <AddDialog :total="total" :addDialog="addDialog" @close-dialog="closeDialog"/>
    <updata-dialog :form="multipleSelection" :updataDialog="updataDialog" @close-dialog="closeDialog"/>
  </div>
</template>

<style scoped>
.job-rank-management {
  padding: 0 20px 20px 20px;
}

.table-operations {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: left;
}

.el-pagination {
  justify-content: flex-end;
}

.el-input__separator {
  margin: 0 5px;
}
</style>